package version_3;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.awt.*;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author 王子铮 11923040205
 */
public class Main extends Application {

    private final TableView<Person> table = new TableView<>();
    final HBox hb = new HBox();

    // 数据源
    // 职工数据源
    private final ObservableList<Person> data_employee =
            FXCollections.observableArrayList(
                    new Person("张三", "China", "15908963661", "2804355025@qq.com"),
                    new Student("李四", "US", "4008823823" , "lisi@sohu.com"),
                    new Faculty("王五", "Japan", "12315", "wangwu@gamil.com")
            );
    // 学生数据源
    private final ObservableList<Person> data_student=
            FXCollections.observableArrayList(
                    new Student("wzz","china","110","wzz@163.com"),
                    new Student("zs","china","112233","zhangsan@163.com")
            );
    // 当前数据源
    // 默认显示的数据源，用户可以自行切换数据源
    private ObservableList<Person> data= data_employee;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {



        Scene scene = new Scene(new Group());
        stage.setTitle("Personnel Information");
        stage.setWidth(750);
        stage.setHeight(600);

        Label label = new Label("Personnel Information  现在是职工列表");
        label.setFont(new Font("AaXiHe", 25));

        table.setEditable(true);

        // 每个Table的列
        TableColumn<Person, String> NameCol = new TableColumn<>("Name");
        // 设置宽度
        NameCol.setMinWidth(100);
        // 设置分箱的类下面的属性名
        NameCol.setCellValueFactory(new PropertyValueFactory<>("Name"));
        // 设置为可编辑的
        NameCol.setCellFactory(TextFieldTableCell.<Person>forTableColumn());
        NameCol.setOnEditCommit((CellEditEvent<Person, String> t) -> {
            ((Person) t.getTableView().getItems().get(t.getTablePosition().getRow())).setName(t.getNewValue());
        });


        TableColumn<Person, String> AddressCol = new TableColumn<>("Address");
        AddressCol.setMinWidth(100);
        AddressCol.setCellValueFactory(new PropertyValueFactory<>("Address"));
        AddressCol.setCellFactory(TextFieldTableCell.<Person>forTableColumn());
        AddressCol.setOnEditCommit((CellEditEvent<Person, String> t) -> {
            ((Person) t.getTableView().getItems().get(t.getTablePosition().getRow())).setAddress(t.getNewValue());
        });

        TableColumn<Person, String> numberCol = new TableColumn<>("Number");
        numberCol.setMinWidth(200);
        numberCol.setCellValueFactory(new PropertyValueFactory<>("number"));
        numberCol.setCellFactory(TextFieldTableCell.<Person>forTableColumn());
        numberCol.setOnEditCommit((CellEditEvent<Person, String> t) -> {
            ((Person) t.getTableView().getItems().get(t.getTablePosition().getRow())).setEmail(t.getNewValue());
        });

        TableColumn<Person, String> emailCol = new TableColumn<>("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(new PropertyValueFactory<>("email"));
        emailCol.setCellFactory(TextFieldTableCell.<Person>forTableColumn());
        emailCol.setOnEditCommit((CellEditEvent<Person, String> t) -> {
            ((Person) t.getTableView().getItems().get(t.getTablePosition().getRow())).setEmail(t.getNewValue());
        });

        // 设置数据源
        table.setItems(data);
        // 添加标签栏
        table.getColumns().addAll(NameCol, AddressCol, numberCol,emailCol);

        // 创建4个文本编辑框
        final TextField addName = new TextField();
        addName.setPromptText("Name");
        // 设置宽度
        addName.setPrefWidth(100);

        //设置添加数据的输入框
        final TextField addAddress = new TextField();
        addAddress.setPrefWidth(100);
        addAddress.setPromptText("Address");

        final TextField addNumber = new TextField();
        addNumber.setPrefWidth(195);
        addNumber.setPromptText("Number");

        final TextField addEmail = new TextField();
        addEmail.setPrefWidth(195);
        addEmail.setPromptText("Email");

        // 创建一个添加按钮
        final Button addButton = new Button("添加");
        // 设置按钮的事件响应
        addButton.setOnAction((ActionEvent e) -> {
            //禁止添加空白数据，否则弹窗提醒
            if (addName.getText().isEmpty()&&addAddress.getText().isEmpty()&&
                    addNumber.getText().isEmpty()&&addEmail.getText().isEmpty()){
                BorderPane pane=new BorderPane();
                Stage stage1=new Stage();
                Label inf=new Label("请勿添加空白数据");
                Button close=new Button("确定");
                close.setOnAction(event -> {
                    stage1.close();
                });
                //设置布局
                pane.setTop(inf);
                pane.setBottom(close);
                stage1.setScene(new Scene(pane,300,100));
                stage1.setTitle("警告！");
                stage1.show();
            }
            else {
                data.add(new Person(
                        addName.getText(),
                        addAddress.getText(),
                        addNumber.getText(),
                        addEmail.getText()
                ));
                // 清空文本
                addName.clear();
                addAddress.clear();
                addNumber.clear();
                addEmail.clear();
            }
        });

        // 创建一个删除按钮
        final Button removeButton = new Button("删除");
        // 设置按钮的事件响应
        removeButton.setOnAction((ActionEvent e) ->{
            if (data.isEmpty()){
                //弹窗警告
                BorderPane pane=new BorderPane();
                Stage stage2=new Stage();
                Label inf=new Label("表中已无数据，请勿重复删除操作！！");
                Button close=new Button("确定");
                close.setOnAction(event -> {
                    stage2.close();
                });
                //设置布局
                pane.setTop(inf);
                pane.setBottom(close);
                stage2.setScene(new Scene(pane,300,100));
                stage2.setTitle("警告！");
                stage2.show();
            }
            else {
                //删除数据
                data.remove(data.size()-1);
            }
        });

        // 设置数据列表切换按钮
        final HBox select_pane = new HBox();
        Button student_button = new Button("切换到学生名单");
        Button employee_button = new Button("切换到职工名单");
        student_button.setOnAction(event -> {
            // 切换数据源
            data=data_student;
            // 设置数据源
            table.setItems(data);
            //设置职工列表标签
            label.setText("Personnel Information  现在是学生列表");
        });
        employee_button.setOnAction(event -> {
            data=data_employee;
            table.setItems(data);
            label.setText("Personnel Information  现在是职工列表");
        });
        select_pane.getChildren().addAll(student_button,employee_button);

        hb.getChildren().addAll(addName, addAddress, addNumber , addEmail, addButton , removeButton);
        hb.setSpacing(3);

        //初始化菜单
        MenuBar menuBar=new MenuBar();

        Menu menuFile=new Menu("Select data");
        Menu menuAbout=new Menu("About");

        MenuItem menu_data_student=new MenuItem("Student data");
        MenuItem menu_data_employee=new MenuItem("Employee data");
        menuFile.getItems().addAll(menu_data_employee,menu_data_student);
        MenuItem menu_about=new MenuItem("about");
        menuAbout.getItems().addAll(menu_about);

        //打开我的gitee仓库
        MenuItem open_gitee=new MenuItem("open my gitee");
        menuAbout.getItems().add(open_gitee);
        open_gitee.setOnAction(event -> {
            try {
                Desktop.getDesktop().browse(new URI("https://gitee.com/wang28043550225/java_homework"));
            } catch (IOException | URISyntaxException e) {
                e.printStackTrace();
            }
        });

        menu_about.setOnAction(event -> {
            BorderPane pane=new BorderPane();
            Stage stage2=new Stage();
            Label inf=new Label("version 3 by wzz 11923040205");
            inf.setFont(Font.font(30));
            Button close=new Button("确定");
            close.setOnAction(e -> {
                stage2.close();
            });
            //设置布局
            pane.setTop(inf);
            pane.setBottom(close);
            stage2.setScene(new Scene(pane,500,100));
            stage2.setTitle("project2 version 3");
            stage2.show();
        });

        menu_data_employee.setOnAction(event -> {
            data=data_employee;
            table.setItems(data);
            label.setText("Personnel Information  现在是职工列表");
        });
        menu_data_student.setOnAction(event -> {
            // 切换数据源
            data=data_student;
            // 设置数据源
            table.setItems(data);
            //设置职工列表标签
            label.setText("Personnel Information  现在是学生列表");
        });

        menuBar.getMenus().addAll(menuFile ,menuAbout);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(menuBar,label,select_pane, table, hb);

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        stage.setScene(scene);
        stage.show();
    }

    public static class Person {

        private final SimpleStringProperty Name;
        private final SimpleStringProperty Address;
        private final SimpleStringProperty number;
        private final SimpleStringProperty email;

        private Person(String name, String address, String number, String email) {
            this.Name = new SimpleStringProperty(name);
            this.Address = new SimpleStringProperty(address);
            this.number = new SimpleStringProperty(number);
            this.email = new SimpleStringProperty(email);
        }

        public String getName() {
            return Name.get();
        }

        public void setName(String name) {
            Name.set(name);
        }

        public String getAddress() {
            return Address.get();
        }

        public void setAddress(String name) {
            Address.set(name);
        }

        public String getNumber() {
            return number.get();
        }

        public void setNumber(String name) {
            number.set(name);
        }

        public String getEmail() {
            return email.get();
        }

        public void setEmail(String name) {
            email.set(name);
        }
    }

    class Student extends Person{
        protected String grade;

        public Student(String name,String address,String phone_number,String email) {
            super(name, address, phone_number, email);
        }
    }

    class Employee extends Person{
        protected String office;
        protected int salary;
        protected String date_engagement;

        public Employee(String name,String address,String phone_number,String email) {
            super(name, address, phone_number, email);
        }

    }

    static class MyDate{
        int year;
        int month;
        int day;
    }

    class Faculty extends Employee{
        //办公时间
        protected String office_hours;
        //级别
        protected String rank;

        public Faculty(String name,String address,String phone_number,String email) {
            super(name, address, phone_number, email);
        }

    }

    class staff extends Employee{
        //职务称号
        String title;

        public staff(String name,String address,String phone_number,String email) {
            super(name, address, phone_number, email);
        }

    }

    class Postgraduate extends Student{
        protected String research_direction;
        protected String tutor;

        public Postgraduate(String name,String address,String phone_number,String email) {
            super(name, address, phone_number, email);
        }
    }
}
